package com.qumitech.visualnovel.service.robottask.impl;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.robottask.RobotTaskConfigDTO;
import com.qumitech.visualnovel.pubdef.dto.robottask.RobotTaskPresentInnerAccountNewDTO;
import com.qumitech.visualnovel.service.robottask.RobotTaskService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class RobotTaskServiceImpl implements RobotTaskService {
    private static final String SaveTaskConfigUrl = "/robottask/config";
    private static final String RmTaskConfigUrl = "/robottask/config";
    private static final String ListTaskConfigUrl = "/admin/robottask/configs";

    private static final String SavePresentAccountUrl = "/robottask/presentaccount";
    private static final String RmPresentAccountUrl = "/robottask/presentaccount/";
    private static final String ListPresentAccountUrl = "/admin/robottask/presentaccount";


    @Override
    public BaseRsp saveTaskConfig(RobotTaskConfigDTO configDTO){
        String url = UrlConstant.SERVER_URL + SaveTaskConfigUrl;
        String rspBody = HttpInvokeUtil.httpPostJson(url, JSONObject.toJSONString(configDTO), UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(rspBody, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        return rsp;
    }

    @Override
    public BaseRsp rmTaskConfig(Integer taskType, Integer configId){
        String url = UrlConstant.SERVER_URL + RmTaskConfigUrl;
        JSONObject req = new JSONObject();
        req.put("task_type", taskType);
        req.put("config_id", configId);
        String rspBody = HttpInvokeUtil.httpDeleteWithEntity(url, JSONObject.toJSONString(req), null, UrlConstant.DefaultEncode);
        BaseRsp rsp = JSONObject.parseObject(rspBody, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        return rsp;
    }

    @Override
    public BaseRsp listTaskConfig(){
        String url = UrlConstant.SERVER_URL + ListTaskConfigUrl;
        String rspBody = HttpInvokeUtil.httpGet(url, null, null);
        BaseRsp rsp = JSONObject.parseObject(rspBody, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        return rsp;
    }

    @Override
    public BaseRsp savePresentInnerAccount(RobotTaskPresentInnerAccountNewDTO newDTO){
        String url = UrlConstant.SERVER_URL + SavePresentAccountUrl;
        String rspBody = HttpInvokeUtil.httpPostJson(url, JSONObject.toJSONString(newDTO), UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(rspBody, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        return rsp;
    }

    @Override
    public BaseRsp listPresentInnerAccount(Long uid, String presentId, Byte accountType,Integer offset,Integer limit){
        String url = UrlConstant.SERVER_URL + ListPresentAccountUrl;
        Map<String, String> paramMap = new HashMap<>();
        if (uid != null){
            paramMap.put("uid", uid.toString());
        }
        if (presentId != null){
            paramMap.put("present_id",presentId);
        }
        if (accountType != null){
            paramMap.put("account_type", accountType.toString());
        }
        if (offset != null){
            paramMap.put("offset", offset.toString());
        }
        if (limit != null){
            paramMap.put("limit", limit.toString());
        }
        String rspBody = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(rspBody, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        return rsp;
    }

    @Override
    public BaseRsp rmPresentInnerAccount(String presentId){
        String url = UrlConstant.SERVER_URL + RmPresentAccountUrl + presentId;
        String rspBody = HttpInvokeUtil.httpDelete(url, new HashMap<>());
        BaseRsp rsp = JSONObject.parseObject(rspBody, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        return rsp;
    }
}
